package com.company.OfferGuide;

public class 礼物的最大价值 {


    public static void main(String[] args) {
        int i = maxValue(new int[][]{{1,2}, {5,6}, {1, 1}});
        System.out.println(i);
    }

    public static int maxValue(int[][] grid) {

        int h = grid.length, w = grid[0].length;
        int[][] map = new int[h][w];
        for (int i = 0; i < w; i++) {
            if (i == 0) map[0][i] = grid[0][i];
            else {
                map[0][i] = grid[0][i] + map[0][i - 1];
            }
        }
        for (int i = 0; i < h; i++) {
            if (i == 0) map[i][0] = grid[i][0];
            else {
                map[i][0] = grid[i][0] + map[i - 1][0];
            }
        }

        for (int i = 1; i < h; i++) {
            for (int j = 1; j < w; j++) {
                map[i][j] = Math.max(grid[i][j] + map[i][j - 1], grid[i][j] + map[i - 1][j]);
            }
        }

        return map[h-1][w-1];

    }

}
